local jinchu = fk.CreateSkill {
  name = "kl__jinchu",
}


jinchu:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic",
  prompt = "#kl__jinchu",
  anim_type = "control",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(jinchu.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if Fk:getCardById(to_select).type == Card.TypeBasic and #selected == 0 then
      return Fk:getCardById(to_select).trueName ~= Fk:cloneCard(self.interaction.data).trueName
    end
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    if #cards ~= 1 then return end
    if not table.find(cards, function(id) return Fk:getCardById(id).type == Card.TypeBasic end) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = jinchu.name
    return card
  end,
})

jinchu:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    return player:hasSkill(jinchu.name) and card and (card:isVirtual() or not card:getEffectiveId())
  end,
  bypass_distances = function(self, player, skill, card)
    return player:hasSkill(jinchu.name) and card and (card:isVirtual() or not card:getEffectiveId())
  end,
})

jinchu:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(jinchu.name) and data.card and (data.card:isVirtual() or not data.card:getEffectiveId())
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
  end,
})

jinchu:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jinchu.name) and player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return player:usedEffectTimes(self.name) == 0
            end
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:askToUseVirtualCard(player, {
      name = Fk:getAllCardNames("b"),
      skill_name = jinchu.name,
      prompt = "#kl__jinchu-use",
      cancelable = true,
      extra_data = {
        extraUse = true,
      },
    })
    player:drawCards(1,jinchu.name)
  end,
})


return jinchu
